#include "NUC_Data.h"
#include "uart_config.h"
#include "string.h"
/*------------------------------------------------------*/	
/*                    接收发送联合体                 */	
/*------------------------------------------------------*/	
static uint8_t  NUC_rx_buf[2][13];
static uint8_t  NUC_tx_buf[NUC_TX_BUF_NUM];
uint8_t  NUC_rx_len;
extern uint16_t time;
/*------------------------------------------------------*/	
/*                       结构体变量                 */	
/*------------------------------------------------------*/	
static NUC_data_t  NUC_data;
//static NUC_data_Tx_t  NUC_data2;

/*------------------------------------------------------*/	
/*                       静态函数声明                */	
/*------------------------------------------------------*/	
static void NUC_data_deal( uint8_t *data_buf, NUC_data_t *NUC);
static void data_transform(uint8_t *data_buf,	NUC_data_t *NUC);

/*------------------------------------------------------*/	
/*                       接收初始化                 */	
/*------------------------------------------------------*/
void   NUC_Sys_Init(void)
{
    NUC_USART_Init(NUC_rx_buf[0],NUC_rx_buf[1],NUC_RX_BUF_NUM,NUC_tx_buf,NUC_TX_BUF_NUM );
}

/*------------------------------------------------------*/	
/*                       获取数据结构               */	
/*------------------------------------------------------*/	
NUC_data_t *get_NUC_data_point(void)
{
    return &NUC_data;
}

/*------------------------------------------------------*/	
/*                         通信串口中断             	*/	
/*------------------------------------------------------*/	

void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        USART_ReceiveData(USART1);
    }
    else if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
    {
			time=0;
        USART_ReceiveData(USART1);
		
        if(DMA_GetCurrentMemoryTarget(DMA2_Stream2) == 0)
        {
            //重新设置DMA
            DMA_Cmd(DMA2_Stream2, DISABLE);
            NUC_rx_len = NUC_RX_BUF_NUM - DMA_GetCurrDataCounter(DMA2_Stream2);
            DMA_SetCurrDataCounter(DMA2_Stream2, NUC_RX_BUF_NUM);
            DMA2_Stream2->CR |= DMA_SxCR_CT;
            //清DMA中断标志
            DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2 | DMA_FLAG_HTIF2);
            DMA_Cmd(DMA2_Stream2, ENABLE);
			
						//数据处理
            NUC_data_deal(NUC_rx_buf[0],&NUC_data);
        }
        else
        {
            //重新设置DMA
            DMA_Cmd(DMA2_Stream2, DISABLE);
            NUC_rx_len = NUC_RX_BUF_NUM - DMA_GetCurrDataCounter(DMA2_Stream2);
            DMA_SetCurrDataCounter(DMA2_Stream2, NUC_RX_BUF_NUM);
            DMA2_Stream2->CR &= ~(DMA_SxCR_CT);
            //清DMA中断标志
            DMA_ClearFlag(DMA2_Stream2, DMA_FLAG_TCIF2 | DMA_FLAG_HTIF2);
            DMA_Cmd(DMA2_Stream2, ENABLE);	
						//数据处理
            NUC_data_deal(NUC_rx_buf[1],&NUC_data);

        }
    }

}
/*------------------------------------------------------*/	
/*                         通信数据处理             */	
/*------------------------------------------------------*/	


static void NUC_data_deal( uint8_t *data_buf, NUC_data_t *NUC)
{
	//判断帧头正确性
   if(data_buf[0] == 0x53)
	{
		//判断帧尾正确性
		if( data_buf[NUC_rx_len-1] == 0x45)
		{
			data_transform(data_buf,NUC);
		}
	}
}

static void data_transform(uint8_t *data_buf,NUC_data_t *NUC)
{

	memcpy(&NUC->NUC_data_Rx.detect_Mark,&data_buf[1],1);
	memcpy(&NUC->NUC_data_Rx.Shoot_Cmd,&data_buf[2],1);
	memcpy(&NUC->NUC_data_Rx.Yaw_Sign,&data_buf[3],1);
	memcpy(&NUC->NUC_data_Rx.Yaw_angle,&data_buf[4],2);
	memcpy(&NUC->NUC_data_Rx.Pitch_Sign,&data_buf[6],1);
	memcpy(&NUC->NUC_data_Rx.Pitch_angle,&data_buf[7],2);
	memcpy(&NUC->NUC_data_Rx.Depth,&data_buf[9],2);
	memcpy(&NUC->NUC_data_Rx.Rece_CRC,&data_buf[11],1);
	
}

 /*------------------------------------------------------*/	
 /*                    发送普通自瞄申请                  */	
 /*------------------------------------------------------*/	
 void  ARMOR_Applicate(NUC_data_t *NUC)
 {
 	NUC->NUC_data_Tx.Curr_Mode=NUC_Aiming_Mode;
 }

 /*------------------------------------------------------*/	
 /*                    发送大能量机关自瞄申请                  */	
 /*------------------------------------------------------*/	
 void  Big_Energy_Applicate(NUC_data_t *NUC)
 {
 	NUC->NUC_data_Tx.Curr_Mode=NUC_BigEnergy_Mode;
 }

 /*------------------------------------------------------*/	
 /*                    发送小能量机关自瞄申请                  */	
 /*------------------------------------------------------*/	
 void  Small_Energy_Applicate(NUC_data_t *NUC)
 {
 	NUC->NUC_data_Tx.Curr_Mode=NUC_SmallEnergy_Mode;
 }

void NUC_SendData(NUC_data_t *NUC)
{
	
    memcpy(&NUC_tx_buf[0],&NUC->NUC_data_Tx,sizeof(NUC->NUC_data_Tx));
//    NUC_tx_buf[0]='S';
//    NUC_tx_buf[21]='E';
    NUC_DMA_TX(22);
}
